import requests
from bs4 import BeautifulSoup #
import time #时间操作
import os
import numpy as np
from io import BytesIO
from selenium import webdriver
import face_recognition
from PIL import Image
from os.path import getsize

class ReptleNetFace:
    def __init__(self):
        basepath = "D:/picture/face/"
        self.dir = "明星/"
        self.local_path = basepath+self.dir
        self.filespath = basepath+"index/files.txt"
        print("开始爬取")
        self.driver = webdriver.Chrome()
        self.driver.get("https://www.baidu.com/s?ie=UTF-8&wd=%E6%98%8E%E6%98%9F")
    def start(self):
        assert "明星" in self.driver.title
        time.sleep(1)
        #获取明星 face和名称
        print("开始获取图片路径")
        urls = []
        names = []
        html = BeautifulSoup(self.driver.page_source,'lxml')
        imgs = html.find_all('img',{'c-img c-img4'})
        pnames = html.find('div',class_='op_exactqa_itemsArea').find_all("p",class_='op_exactqa_item_img')

        for index in range(len(imgs)):
            try:
                urls.append(imgs[index]['src'])
                names.append(pnames[index].a['title'])
            except:
                continue
        print("开始保存图片++++++")
        self.create_dir()
        self.saveNameAndFace(urls,names)

        self.netPage()
    def netPage(self):
        time.sleep(1)
        elem = self.driver.find_element_by_class_name("opui-page-next")
        elem.click()
    def saveNameAndFace(self,urls,names):
        for i,url in enumerate(urls) :
            time.sleep(1)
            image = self.load_web_photo(url)
            #下载图片
            filename = names[i]+'.jpg'
            num = 1
            while True:
                filepath = self.local_path+filename
                if not os.path.exists(filepath):
                    self.saveFace(filepath,image)

                    # f = open(filepath,'ab')
                    # f.write(img.content)
                    # print(filepath,'图片保存成功！')
                    # f.close()
                    #保存名称
                    ft = open(self.filespath,'ab')
                    ft.write(("\t"+self.dir+filename).encode())
                    ft.close()
                    break
                else:
                    filename = names[i]+str(num)+'.jpg'
                    num+=1
                    continue
    # 创建存放图片的文件夹,并下载图片
    def create_dir(self):
        print(self.local_path)
        if not os.path.abspath(self.local_path):
            os.mkdir(self.local_path)
    def saveFace(self,filepath,image):
        # image = self.load_web_photo(url)
        face_locations = face_recognition.face_locations(image)

        for face_location in  face_locations:
            flag = 1
            top,right,bottom,left =  face_location
            #反复测试最佳的脸部位置
            while flag==1:
                try:
                    face_image = image[top:bottom,left:right]
                    face_img = Image.fromarray(face_image)
                    face_img.save(filepath)
                    face_recognition.face_encodings(face_recognition.load_image_file(filepath))[0]
                    #压缩图片不大于5kb
                    while True:
                        if(getsize(filepath)>=5120):
                            w, h = face_img.size
                            # face_img.resize((w//2, h//2),Image.ANTIALIAS)  #缩
                            face_img.thumbnail((h//2,w//2))
                            face_img.save(filepath)
                            continue
                        else:
                            break
                    flag = 0
                except IndexError:
                    top-=1;right+=1;bottom+=1;left-=1
                except ValueError:
                    continue
    def load_web_photo(self,url, mode='RGB'):
        rq = requests.get(url)
        im = Image.open(BytesIO(rq.content))
        if mode:
            im = im.convert(mode)
        return np.array(im)
rnf = ReptleNetFace()
numw = 0
while numw <100:
    rnf.netPage()
    if numw>=47:
        print(numw)
        rnf.start()
    # rnf.start()
    numw+=1